<?php
/**
 * @abstract All the resources have to be one word
 * 
 */
class User_SearchController extends Clu_Controller_Action_Master
{
	public $m; //model

	public function init()
	{
		$this->m = new user_Model_User();
		$this->mRole = new user_Model_Role();
		parent::init();
	}
    /**
     * @abstract: Search users
     */
    public function searchAction()
    {
    	$this->viewParams['form'] = new user_Form_SearchUser();
        $actionUrl = $this->view->hvnUrl(array('module' => 'user', 'action'=>'search'), 'nocontroller');
        $this->viewParams['form']->cluBuild(array('action' => 'search', 'action_url' => $actionUrl));
        
        if ($this->_request->isPost())
        {
        	$data = $this->_request->getParams();
        	if (!isset($data['user_perpage']))
    			$data['user_perpage'] = 10;
    		if (!isset($data['page']))
    			$data['page'] = 1;
		   	$select = $this->m->search($data);
			$paginator = $this->generatePaginator($select, $data['page'], $data['user_perpage'], 7, 'Jumping');
			$this->viewParams['form']->populate($data);
			
	       	//cache in $_SESSION
	       	$sess = new Zend_Session_Namespace("user_paginator");
	       	$sess->user_admin = array("user_search_params" => $data);
        }
        else
        {
			//if (!empty($data['page']) )
	        {
	           $sess = new Zend_Session_Namespace("user_paginator");
	           $data = $sess->user_admin;
	           if(isset($data))
	           {
           			$select = $this->m->search($data['user_search_params']);
           			$paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['user_search_params']['user_perpage'], 
           													7, 'Jumping');
           			$this->viewParams['form']->populate($data['user_search_params']);
	           }
	        }
        }
       
	    //the results
	    if(isset($paginator))
	    {	
		    $this->viewParams['paginator'] = $paginator;
			$this->viewParams['userList'] = $paginator->getCurrentItems();
			
			//get userRoles
			$userRoles = array();
            foreach ($this->viewParams['userList'] as $k=>$user) 
            {
                $roles = $this->m->getUserRoleData($user);
                $userRoles[$user['id']] = '';
                foreach ($roles as $role)
                {
                    $userRoles[$user['id']]  = $role['rname'] . ', ' . $userRoles[$user['id']] ;
                }
            }
            $this->viewParams['userRoles'] = $userRoles;
        }
    }
    
}
?>